<?php

/**
* @Author:  	Daniel Dinh
* @Email:   	webdevelopmentvn@gmail.com
* @Link: 		http://joomsoft.net
* @Version: 	1.0
* @copyright:	Joomsoft.net
* @license:		GNU Lesser GPL
*/

// No direct access
defined('_JEXEC') or die;

jimport('joomla.application.component.controllerform');

class FarmacityControllerPeriod extends JControllerForm
{
	/**
	 * @var    string  The prefix to use with controller messages.
	 * @since  1.6
	 */
	protected $text_prefix = 'COM_FARMACITY_PERIOD';

	/**
	 * Method override to check if you can add a new record.
	 *
	 * @param   array  $data  An array of input data.
	 *
	 * @return  boolean
	 *
	 * @since   1.6
	 */
	
	 
	protected function allowAdd($data = array())
	{
		// Initialise variables.
		$user		= JFactory::getUser();
		if ($allow === null)
		{
			// In the absence of better information, revert to the component permissions.
			return parent::allowAdd($data);
		}
		else
		{
			return $allow;
		}
	}

	
	function save()
	{
		$db = & JFactory::getDBO();
		$userid = & JFactory::getUser()->id;
		$app = & JFactory::getApplication();
		
		$id = $_POST['jform']['id'];
		
		
		$from_day = $_POST['dal_day'];
		$from_month = $_POST['dal_month'];
		$to_day = $_POST['al_day'];
		$to_month = $_POST['al_month'];
		
		$year = 2012;
		$from_full = $year.'-'.$from_month.'-'.$from_day;
		$to_full = $year.'-'.$to_month.'-'.$to_day;
		
		$_SESSION['jform'] = $_POST['jform'];
		$_SESSION['jform']['to'] = $to_full;
		$_SESSION['jform']['from']  = $from_full;
		// check date
		if(!$this->checkDate($from_day,$from_month))
		{
			JError::raiseWarning(403, JText::_('COM_FARMACITY_ERROR_DATE_INVALID'));
			$app->redirect(JRoute::_("index.php?option=com_farmacity&task=period.edit&id=$id",0,2));
		}
		if(!$this->checkDate($to_day,$to_month))
		{
			JError::raiseWarning(403, JText::_('COM_FARMACITY_ERROR_DATE_INVALID'));
			$app->redirect(JRoute::_("index.php?option=com_farmacity&task=period.edit&id=$id",0,2));
		}
		
		if(strtotime($to_full) <= strtotime($from_full))
		{
			JError::raiseWarning(403, JText::_('COM_FARMACITY_ERROR_DATE_LESS_THAN'));
			$app->redirect(JRoute::_("index.php?option=com_farmacity&task=period.edit",0,2));
		}
		if($id)
			$query = "select id from #__far_periodi where id<>$id AND '$from_full' < al AND '$to_full' > dal  and id_user=".$userid;
		else
			$query = "select id from #__far_periodi where  '$from_full' < al AND '$to_full' > dal  and id_user=".$userid;
		
		$db->setQuery($query);
		
				//echo $db->getQuery();exit;
				
				
		if($db->loadResult())
		{
			JError::raiseWarning(403, JText::_('COM_FARMACITY_ERROR_DATE_VIOLATION'));
			$app->redirect(JRoute::_("index.php?option=com_farmacity&task=period.edit",0,2));
		}
		if($id)
			$db->setQuery("select id from #__far_periodi where id<>$id AND id_user =$userid and title=".$db->Quote($_POST['jform']['title']));
		else
			$db->setQuery("select id from #__far_periodi where id_user =$userid and title=".$db->Quote($_POST['jform']['title']));


		if($db->loadResult())
		{
			JError::raiseWarning(403, JText::_('COM_FARMACITY_ERROR_NAME_DUPLICATE'));
			$app->redirect(JRoute::_("index.php?option=com_farmacity&task=period.edit&id=$id",0,2));
		}
		else
		{
			
			$_POST['jform']['dal'] = $from_full;
			$_POST['jform']['al'] = $to_full;
			unset($_SESSION['jform']);
			parent::save();
		}
	}

	private function checkDate($day,$month)
	{
		$day = intval($day);
		$month = intval($month);
		$res = true;
		switch($month)
		{
			case 4:
			case 6:
			case 9:
			case 11:
				if($day>30)
					$res = false;
				break;
			case 2:
				if($day>29)
					$res = false;
				break;
			default:
				$res = true;
				break;
		}
		return $res;
	}
}
